package com.zzhua.controller;

import com.zzhua.entity.Student;
import com.zzhua.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("findAll")
    public Collection<Student> findAll(){
        return studentRepository.findAll();
    }

    @GetMapping("findById/{id}")
    public Student findById(@PathVariable(name = "id") Integer id) {
        return studentRepository.findById(id);
    }

    @PostMapping("add")
    public String add(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
        return "ok";
    }

    @PutMapping("updateById")
    public String updateById(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
        return "ok";
    }

    @DeleteMapping("deleteById/{id}")
    public String deleteById(@PathVariable(name = "id") Integer id){
        studentRepository.deleteById(id);
        return "ok";
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("port")
    public String getPort() {
        return port;
    }


}
